<template>
	<view class="charts-container">
		<qiun-data-charts 
		:chartData="chartData" 
		:opts="opts" 
		type="line"
		></qiun-data-charts>
	</view>
	
</template>

<script setup>
	import {ref,reactive} from "vue"
	import {onReady,onLoad} from "@dcloudio/uni-app"
	const chartData  = ref()
	const opts = ref({
		yAxis:{
			showTitle:true,
			data:[
				{title:""}
			]
		},
		xAxis:{
			title:"时间",
			titleOffsetX:-26
		}
	})
	let	type = ""
	let prop = "" 
	let userId = ""
	const getData = async ()=>{
		const category = []
		let unit = ""
		let series = [
		  {name:title,data:[]}
		]
		const isBloodGlucose = type<=3?true:false
	
		const params = {
		  userId,
		  type:isBloodGlucose?12:13
		}
		isBloodGlucose&&(params.originalFoodStatus=Number(type))
		let {data} = await $api.getSugarListByUserId(params)
		
		switch(type){
		  case "0":case "1":case "2":
			unit = data[0].indicatorUnits
			break
		  case "3":
			unit = data[0].pIndicatorUnits
			break
		  case "4":
			unit = data[0].sbpIndicatorUnits
			break
		  case "5":
			unit = data[0].dbpIndicatorUnits
			break
		  case "6":
			unit = data[0].bpIndicatorUnits
		}
		series.forEach(item=>{
		  const date = new Date(item.createTime)
		  category.push(`${date.getMonth()+1}/${date.getDate()} ${date.getHours()}时`)
		  series[0].data.push(item[prop])
		})
		if(type==="6"){
			// 类型为6有两个图
		  let arr = []
		  series[0].data.map(item=>item.split("/")).forEach(item=>{
			arr[0]?arr[0].push(item[0]):arr[0]=[item[0]]
			arr[1]?arr[1].push(item[1]):arr[1]=[item[1]]
		  })
		  series[0].name = "最高血压"
		  series[0].data = arr[0]
		  series[0].name = "最低血压"
		  series[0].data = arr[1]
		}
		chartData.value = {
			category,
			series
		}
		opts.value.yAxis.data[0].title = unit
	}

	
	onLoad((options)=>{
	    type = options.type
		userId = options.userId
		let title = ""
	    switch(type){ //
	      case "0":
	        title="餐前血糖"
	        prop = "indicatorResult"
	        break
	      case "1":
	        title="餐后血糖"
	        prop = "indicatorResult"
	        break
	      case "2":
	        title="随机血糖"
	        prop = "indicatorResult"
	        break
	      case "3":
	        title="脉搏"
	        prop="pIndicatorResult"
	        break
	      case "4":
	        title="收缩压"
	        prop="sbpIndicatorResult"
	        break
	      case "5":
	        title="舒张压"
	        prop="dbpIndicatorResult"
	        break
	      case "6":
	        title="血压"
	        prop="bpIndicatorResult"
			break
	    }
	    uni.setNavigationBarTitle({
	      title
	    })
	})
	onReady(()=>{
		getData()
		// chartData.value = {
		// 	categories:[1,2,3,4,5],
		// 	series:[
		// 		{	
		// 			name:"第一个",
		// 			data:[1,2,3,4,5],
		// 		},
		// 		{
		// 			name:"第二个",
		// 			data:[2,3,4,5,6],
		// 		}
		// 	]
		// }
	})
</script>

<style lang="scss">
	.charts-container{
		box-sizing: border-box;
		height: 100vh;
		padding:10rpx 20rpx 20rpx;
		padding-left: constant(safe-area-inset-bottom);
		padding-left: env(safe-area-inset-bottom);
	}
</style>